<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>添加用户界面</title>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/bootstrap.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/layui/css/layui.css"/>
		<link rel="stylesheet" type="text/css" href="../../../resource/css/niubility.css"/>
		<script type="text/javascript" src="../../../resource/layui/layui.js"></script>
		<script src="../../../resource/js/static_url.js"></script>
		<style type="text/css">
			.form-control{
				width: 300px;
			}
			#btn{
			}
			.boxOne{
				float: left;
				margin-left: 10px;
			}
			/* 提交成功之后，提示消息的位置 */
			.tip_position{
				position: fixed !important;
				left: 50% !important;
				top: 50% !important;
				margin-left: -283px !important;
				margin-top: -80px !important;
				background: rgba(0,0,0,0.7) !important;
				color: #fff3cd !important;
			}
		</style>
	</head>
	<body>
		<form id="form1" action="" method="post" >
			<div class="boxOne">
				<div class="form-group">
					<label for="teacherOffice">用户类别</label>
					<select class="form-control" id="userCategoryNo" name="userCategoryNo">
						<option value="02">普通用户</option>
					</select>
				</div>

				<div class="form-group">
					<label for="username">用户姓名</label>
					<input type="text" name="userName" class="form-control" id="username" required minlength="1" maxlength="10" />
				</div>

				<div class="form-group">
					<label for="userNo">用户编号</label>
					<input type="text" name="userNo" class="form-control" id="userNo" >
				</div>

				<div class="form-group">
					<label for="teacherOffice">研究室</label>
					<select class="form-control" id="teacherOffice" name="teacherOfficeNo">
						<option value="-1">--未选择--</option>
					</select>
				</div>
				<!--填写密码-->
				<div class="form-group">
					<label for="pass" >密码</label>
					<input type="password" name="password" class="form-control" id="pass" placeholder="请输入密码" required minlength="1" maxlength="16">
				</div>
				<!--确认密码-->
				<div class="form-group">
					<label for="confirPass">确认密码</label>
					<input type="password" name="confirmPassword" class="form-control" id="confirPass" placeholder="再一次填写密码" required minlength="1" maxlength="16">
				</div>
			</div>
			<div style="clear: both;"></div>
			<button type="button" id="btn" class="btn btn-default" onclick="mysubmit()">提交</button>
		</form>
		<script type="text/javascript" src="../../../resource/js/jquery.min.js"></script>	
		<script type="text/javascript">
			//将实验室下拉列表进行渲染
			$.ajax({
				type:'get',
				url: search_room,
				async: true,
				success: function(info){
				    if(info.code != 0)
					{
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.msg(info.msg, {skin:'tip_position',time:'3000'});
                        });
					    return;
					}

				    var data = info.data; // data是一个数组
					//每一次添加数据之前，要进行“清空”，因为append没有覆盖功能
                    $("#teacherOffice").empty();
					for(var p in data)
					{
					    $("#teacherOffice").append("<option value="+data[p].id+">"+data[p].name+"</option>");
					}
				},
				error:function(){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.msg('系统错误！！！', {skin:'tip_position',time:'3000'});
                    });
				}
			});


			function mysubmit(){

				//用户名：（1）用户姓名不能为空（2）长度不超过16位（3）职工姓名不用限制
				var userName =  $("#username").val();
				if(userName==""){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，请填写用户姓名 ', '#username', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
				}else if(userName.length>16){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，名字输入过长 ', '#username', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
				}
				/* ----限制只能输入汉字
				var zz = /^[\u4e00-\u9fa5]+$/;
				if( !zz.test(userName) ){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，用户姓名只能输入汉字 ', '#username', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
					return;
				}*/
				
				
				//3. 用户编号： （1）不为空（2）长度不能大于8位（3）职工编号只有数字
				var userNo = $("#userNo").val();
                if(userNo == ""){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，请填写用户编号 ', '#userNo', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
                }else if(userNo.length != 8){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，职工编号只为能为8位 ', '#userNo', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
                }
                // 职工编号只能为数字
				for(let i=0;i<userNo.length;i++){
					//取出字符串中，每一个字符
					var sOne =  userNo.charAt(i);
					if(!(sOne>=0 && sOne<=9)){
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.tips(' 亲，职工编号只能为数字 ', '#userNo', {
                                tips: [1, '#333'],
                                time: 2500,
                            });
                        });
						return;
					}
				}


				// 密码：（1）不为空（2）长度不超过16位 （3）数字+英文
				var pass = $("#pass").val();
                if(pass == ""){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，请填写密码 ', '#pass', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
                }else if(pass.length>16){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，密码不能超过16位 ', '#pass', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
                }
				for(let i=0;i<pass.length;i++)
				{
				    //取出每一位字符进行判断
                    var sOne =  pass.charAt(i);
                    //英文字符有大小写的区分，为了减少判断条件，进行小写转换
					sOne = sOne.toLocaleLowerCase();
                    if( !(sOne>=0&&sOne<=9 || sOne>='a'&&sOne<='z') ){
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.tips(' 亲，密码只能为英文和数字，不能包含特殊字符 ', '#pass', {
                                tips: [1, '#333'],
                                time: 2500,
                            });
                        });
						return;
					}
				}


				//5. 确认密码
				var confirPass = $("#confirPass").val();
				if(confirPass == ''){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，确认密码没有填 ', '#confirPass', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
                    return;
				}
				if(pass != confirPass){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.tips(' 亲，密码和确认密码不一致 ', '#confirPass', {
                            tips: [1, '#333'],
                            time: 2500,
                        });
                    });
				    return;
				}
				//最后就可以提交数据
				$.ajax({
					type:'post',
					url: admin_add_user,
					async: true,
					dataType:"json",
					data:$("#form1").serialize(),
					success:function(info){
					    if(info.code != 0 ){
                            layui.use('layer', function(){
                                var layer = layui.layer;
                                layer.msg(info.msg, {skin:'tip_position',time:'3000'});
                            });
						}
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.msg(info.msg, {skin:'tip_position',time:'3000'});
                        });

					    // 清空文本框
						for(let i=0; i<$(".boxOne input").length; i++)
						{
                            $(".boxOne input").eq(i).val("");
						}
					},
					error:function(){
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.msg('系统错误！！！', {skin:'tip_position',time:'3000'});
                        });
					}
				});
			}
		</script>
	</body>
</html>
